package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.beans.Login;
import br.ucb.sigplan.beans.Turma;
import br.ucb.sigplan.crud.ProfessorCrud;
import br.ucb.sigplan.crud.TurmaCrud;

public class AbrirListarTurmaProfessorAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Login login = (Login) req.getSession().getAttribute("login");
		Integer idProfessor;
		if (login.getTipo().getValue() == 'd') {
			idProfessor = new Integer(req.getParameter("idProfessor"));
		}else {
			idProfessor = ProfessorCrud.findByEmail(login.getEmail()).getIdProfessor();
		}
		List<Turma> turmas = TurmaCrud.findByProfessor(idProfessor);
		if (turmas.isEmpty()) {
			req.setAttribute("msg", "turmasVazio");
			return "/jsp/index.jsp";
		}
		if (req.getParameter("acao").equalsIgnoreCase("plano")) {
			req.getSession().setAttribute("turmas", turmas);
			return "/jsp/professor/plano/profEscolheTurma.jsp";
		}else if (req.getParameter("acao").equalsIgnoreCase("verTurma")){
			req.setAttribute("turmas", turmas);
			return "/jsp/diretor/turma/dirListaTurmas.jsp";
		}else{
			req.setAttribute("turmas", turmas);
			return "/jsp/professor/turma/listagemTurma.jsp";
		}
	}

}
